package com.saver.modules.sys.service;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.DateUtils;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.Query;
import com.saver.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


/**
 * 日志
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月30日 下午10:45
 */
@Service("sysLogService")
public class SysOperationLogService {

    @Autowired
    private JdbcTool jdbcTool;


    public PageUtils queryPage(Map<String, Object> params) {
        String sql = "select id,username,operation,method,time,ip,browser,browser_version,monitor_system,create_date from sys_operation_log t where 1=1";
        String username = (String) params.get("username");
        String operation = (String) params.get("operation");
        String createDate = (String) params.get("createDate");
        if (username != null && !username.equals("")) {
            sql = sql + " and (username like '%" + username + "%')";
        }
        if (operation != null && !operation.equals("")) {
            sql = sql + " and (operation like '%" + operation + "%')";
        }
        if (createDate != null && !createDate.equals("")) {
            String[] split = createDate.split(",");
            Date start = new Date(Long.parseLong(split[0]));
            Date end = new Date(Long.parseLong(split[1]));
            String startFormat = DateUtils.format(start, DateUtils.DATE_TIME_PATTERN);
            String endFormat = DateUtils.format(end, DateUtils.DATE_TIME_PATTERN);
            sql = sql + " and (create_date >= '" + startFormat + "' and create_date <='" + endFormat + "')";
        }
        Query query = new Query(params);
        return jdbcTool.queryPage(sql, query);
    }

    public R getParams(Long id) {
        Map<String, Object> map = jdbcTool.get("select params from sys_operation_log t where t.id=" + id);
        return R.ok().put("data", map);
    }
}
